<?php
namespace app\blog\controller\admin;

use app\common\controller\Back;

class Type extends Back
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        $args = [
            'where' => [],
            'order' => [
                'type_parent' => 'desc',
                'type_weight' => 'desc',
                'type_id' => 'desc',
            ],
        ];
        //状态
        if($this->query['status']){
            if(in_array($this->query['status'],model('common/Attr','loglic')->statusKeys())){
                array_push($args['where'],['type_status','eq',$this->query['status']]);
            }
        }
        //搜索
        if($this->query['searchText']){
            array_push($args['where'],['type_name|type_excerpt','like','%'.$this->query['searchText'].'%']);
        }
        //查询数据
        $data = model('blog/Type','loglic')->allLevel($args);
        //模板变量赋值
        $this->assign($this->site);
        $this->assign('data',$data);
        $this->assign('query',$this->query);
        //渲染模板输出
        return $this->fetch();
    }

    public function create()
    {
        $form = xfFormCreate([
            'name'      => 'blogAdminTypeCreate',
            'action'    => xfUrl('blog/admin.type/save'),
            'class'     => 'row',
            'reset'     => '重置',
            'items'     => model('blog/Type','form')->items([
                'type_weight' => model('blog/Type','loglic')->getWeightMax(),
            ]),
        ]);
        $this->assign('form', $form);
        $this->assign($this->site);
        return $this->fetch();
    }
    public function save()
    {
        $model = model('blog/Type','loglic');
        $result = $model->dataSave(input('post.','a'));
        if(!$result){
            $this->error($model->getError());
        }
        $this->success('成功','blog/admin.type/index');
    }

    public function delete()
    {
        $result = model('common/Type','loglic')->deleteId(input('id/d',0));
        if($result){
            $this->success('成功');
        }else{
            $this->error(model('common/Type','loglic')->getError());
        }
    }

    public function deletes()
    {
        foreach(input('ids/a') as $key=>$typeId){
            model('common/Type','loglic')->deleteId($typeId);
        }
        $this->success('成功');
    }
    
    public function edit()
    {
        if(!$id = input('id/d',0)){
            $this->error($this->error,'blog/admin.type/index');
        }
        //查询数据
        $result = model('blog/Type','loglic')->getId($id);
        if(!$result){
            $this->error($this->error,'blog/admin.type/index');
        }
        $form = xfFormCreate([
            'name'         => 'blogAdminTypeEdit',
            'action'       => xfUrl('blog/admin.type/update'),
            'items'        => model('blog/Type','form')->items($result),
            'reset'        => '重置',
            'class'        => 'row',
        ]);
        $this->assign('form',$form);
        $this->assign($this->site);
        return $this->fetch();
    }
    public function update()
    {
        $model = model('blog/Type','loglic');
        $result = $model->dataUpdate(input('post.'));
        if(!$result){
            $this->error($model->getError());
        }
        $this->success('成功','blog/admin.type/index');
    }

    //快速排序
    public function weight()
    {
        $model = model('blog/Type','loglic');
        $result = $model->weightUpdate(input('id/d'),input('value/d',0));
        if(!$result){
            $this->error($model->getError());
        }
        $this->success('成功');
    }

    //批量审核
    public function normal()
    {
        $result = model('common/Type','loglic')->fieldUpdate(input('ids/a'),'type_status','normal');

        $this->success('成功');
    }

    //批量隐藏
    public function hidden()
    {
        $result = model('common/Type','loglic')->fieldUpdate(input('ids/a'),'type_status','hidden');

        $this->success('成功');
    }
}